/**
 * This class creates the Organizational and sub process-->AppService graphs
 *
 */
class ServicesListCreator
{
    Organization org
    Logger logger = new Logger()

    ServicesListCreator(Organization org)
    {
        this.org = org
    }

    /**
     * creates and returns the JS list for the list of services
     */
    def createBPTimelineList()
    {
        logger.info("Generating the list views for the Organization:"+org.name)
        
        def ctr = 1
        def row = ""
        def rows = []
        def str1 = "var list=[\n"
        def str2 = "];\n"
        def str = ""

        org.orgServices.each { key, myOrgService -> 
            //println  "\t|----OS:" + myOrgService.name

            myOrgService.businessFunctions.each { bfkey, myBusinessFunction ->
                //println "\t\t|----BF:" + myBusinessFunction.name

                myBusinessFunction.businessProcesses.each { bpkey, myBusinessProcess ->
                    //println "\t\t\t|----BP:" + myBusinessProcess.name
                    //println "\t\t\t\t\t|-----timeline:" + myBusinessProcess.timelineItem
                    //println "\t\t\t\t|----SP:" + mySubProcess.name

                    row = "['" + ctr + "', '" + myOrgService.name + "', '" + myBusinessFunction.name + "', '" + myBusinessProcess.name + "', '" + myBusinessProcess.timelineItem + "']"
                    rows.add(row)
                    str = str + row + ",\n"
                    ctr++

                    /*
                    myBusinessProcess.subProcesses.each { spKey, mySubProcess ->
                        mySubProcess.appServices.each { appSvcKey, myAppService ->
                            println "\t\t\t\t\t|----AppSvc:" + myAppService.name
                        }
                        
                    }
                    */
                }
            }
        }

        // create the full string
        str = str1 + str + str2

        def strBuf = new StringBuffer(str)
        strBuf.setCharAt(str.lastIndexOf(','), ' ' as char)

        return strBuf.toString()
    }

    /**
     * creates and returns the JS list for the list of services
     */
    def createSPTimelineList()
    {
        logger.info("Generating the list views for the Organization:"+org.name)
        
        def ctr = 1
        def row = ""
        def rows = []
        def str1 = "var list=[\n"
        def str2 = "];\n"
        def str = ""

        org.orgServices.each { key, myOrgService -> 
            //println  "\t|----OS:" + myOrgService.name

            myOrgService.businessFunctions.each { bfkey, myBusinessFunction ->
                //println "\t\t|----BF:" + myBusinessFunction.name

                myBusinessFunction.businessProcesses.each { bpkey, myBusinessProcess ->
                    //println "\t\t\t|----BP:" + myBusinessProcess.name
                    //println "\t\t\t\t\t|-----timeline:" + myBusinessProcess.timelineItem
                    
                    myBusinessProcess.subProcesses.each { spKey, mySubProcess ->

                        //println "\t\t\t\t|----SP:" + mySubProcess.name
                        row = "['" + ctr + "', '" + myOrgService.name + "', '" + myBusinessFunction.name + "', '" + myBusinessProcess.name + "', '" + mySubProcess.name + "', '" + mySubProcess.timelineItem + "']"
                        rows.add(row)
                        str = str + row + ",\n"

                        ctr++

                        //println ctr + "."+ row
                        //ctr++

                        /*
                        mySubProcess.appServices.each { appSvcKey, myAppService ->
                            println "\t\t\t\t\t|----AppSvc:" + myAppService.name
                        }
                        */
                    }
                }
            }
        }

        // create the full string
        str = str1 + str + str2

        def strBuf = new StringBuffer(str)
        strBuf.setCharAt(str.lastIndexOf(','), ' ' as char)

        return strBuf.toString()
    }

    /**
     * creates and returns the JS list for the list of services
     */
    def createAppServicesList()
    {
        logger.info("Generating the list views for the Organization:"+org.name)

        def ctr = 1
        def row = ""
        def rows = []
        def str1 = "var list=[\n"
        def str2 = "];\n"
        def str = ""

        org.orgServices.each { key, myOrgService -> 
            //println  "\t|----OS:" + myOrgService.name

            myOrgService.businessFunctions.each { bfkey, myBusinessFunction ->
                //println "\t\t|----BF:" + myBusinessFunction.name

                myBusinessFunction.businessProcesses.each { bpkey, myBusinessProcess ->
                    //println "\t\t\t|----BP:" + myBusinessProcess.name
                    //println "\t\t\t\t\t|-----timeline:" + myBusinessProcess.timelineItem

                    myBusinessProcess.subProcesses.each { spKey, mySubProcess ->

                        //println "\t\t\t\t|----SP:" + mySubProcess.name

                        mySubProcess.appServices.each { appSvcKey, myAppService ->
                            //println "\t\t\t\t\t|----AppSvc:" + myAppService.name

                            row = "['" + ctr + "', '" + myBusinessProcess.name + "', '" + mySubProcess.name + "', '" + mySubProcess.timelineItem + "', '" + myAppService.name + "', '" + myAppService.sourceSystemName + "', '" + myAppService.sourceAppComponentName + "', '" + myAppService.sinkSystemName + "', '" + myAppService.sinkAppComponentName + "', '" + myAppService.dataObjectType + "', '" + myAppService.dataObjectName + "']"

                            rows.add(row)
                            str = str + row + ",\n"
                            
                            ctr++
                        }
                    }
                }
            }
        }

        // create the full string
        str = str1 + str + str2

        def strBuf = new StringBuffer(str)
        strBuf.setCharAt(str.lastIndexOf(','), ' ' as char)

        return strBuf.toString()
    }
}

